﻿#ifndef QLYAVCODECCONTEXT_H
#define QLYAVCODECCONTEXT_H

#include <QQueue>
#include "QlyAVFrame.h"
#include "QlyAVPacket.h"

extern "C" {
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include <libavutil/opt.h>
}

class QlyAVCodec;

class QlyAVCodecContext
{
public:
    QlyAVCodecContext();
    ~QlyAVCodecContext();

    int opt_set(const char *name, const char *val, int search_flags);

    bool allocContext(QlyAVCodec *pCodec);
    bool decodePacket(const AVPacket *pkt, QQueue<QlyAVFrame> &queue);
    bool encodeFrame(const AVFrame * const pFrame, QQueue<QlyAVPacket> &queue);
    AVCodecContext	* ptr() {return pCodecCtx;}
    bool openCodec(AVCodec *pCodec, const AVCodecParameters *codecpar);
    bool openCodec(QlyAVCodec *pCodec, const AVCodecParameters *codecpar);
    void setWidth(int width);
    void setHeight(int height);
    void setTimeBase(AVRational timebase);
    void setFrameRate(AVRational rate);
    void setFrameRate(double rate);
    void setBitRate(int64_t bitRate);
    void setPixelFormat(AVPixelFormat format);
    void setMaxBFrame(int n);
    void setGopSize(int size);
    int width() const;
    int height() const;
    AVRational timeBase();
    int64_t bitRate() const;

private:
    int sendFrame(QlyAVFrame frame);
    AVCodecContext	*pCodecCtx = nullptr;
    int errorcode = 0;
};

#endif // QLYAVCODECCONTEXT_H
